package com.briup.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;

//统计词频
public class WordCount extends Configured implements Tool {
    static class WorkCountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            String[] words = value.toString().split("[,]");
            for (String word : words) {
                context.write(new Text(word),new IntWritable(1));
            }
        }
    }
    static class WordCountReducer extends Reducer<Text, IntWritable,Text, IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int sum =0;
            for (IntWritable val : values) {
                sum +=val.get();
            }
            context.write(key,new IntWritable(sum));
        }
    }
    @Override
    public int run(String[] strings) throws Exception {
        //构建配置
        Configuration conf = getConf();
        //指定输入路径和输出路径
        Path input = new Path("hdfs://192.168.10.11:9000/workdata");
        Path output = new Path("hdfs://192.168.10.11:9000/workoutput");
//        String inData = conf.get("input");
//        Path input = new Path(inData);
//        String outData = conf.get("output");
//        Path output = new Path(outData);
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.10.11:9000"), conf);
        if (fileSystem.exists(output)){
            fileSystem.delete(output,true);
        }
        //构建job
        Job job = Job.getInstance(conf);
        //设置job名
        job.setJobName("world");
        //设置job主类
        job.setJarByClass(this.getClass());
        //设置map配置 主类  规定K2 V2 类型
        job.setMapperClass(WorkCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //设置reduce配置  主类 K3 V3类型
        job.setReducerClass(WordCountReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //设置输入处理器
        job.setInputFormatClass(TextInputFormat.class);
        //输入处理器
        TextInputFormat.addInputPath(job,input);
        //输出处理器
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,output);

        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new WordCount(),args));
    }
}
